package com.community.web.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.model.entity.Goods;
import com.community.model.entity.GoodsFavorites;
import com.community.model.entity.GoodsPicture;
import com.community.web.user.mapper.*;
import com.community.web.user.service.GoodsService;
import com.community.web.user.vo.GoodsDetailVo;
import com.community.web.user.vo.GoodsQueryVo;
import com.community.web.user.vo.GoodsVo;
import com.community.web.user.vo.StoreInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author admin
* @description 针对表【goods】的数据库操作Service实现
* @createDate 2024-07-02 10:56:56
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService{

    @Autowired
    private GoodsMapper mapper;

    @Autowired
    private GoodsPictureMapper pictureMapper;

    @Autowired
    private GoodsFavoritesMapper favoritesMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private GoodsStoreMapper storeMapper;

    @Override
    public IPage<GoodsVo> pageGoodsList(Page<GoodsVo> page, GoodsQueryVo queryVo) {
        return mapper.pageGoodsList(page, queryVo);
    }

    @Override
    public GoodsDetailVo goodsDetail(long id, Long userId) {
        //商品信息
        Goods goods = mapper.selectById(id);
        //商品图片列表
        LambdaQueryWrapper<GoodsPicture> pictureQueryWrapper = new LambdaQueryWrapper<>();
        pictureQueryWrapper.eq(GoodsPicture::getGoodsNo, goods.getGoodsNo());
        pictureQueryWrapper.eq(GoodsPicture::getPictureType, 1);
        List<GoodsPicture> goodsPictures = pictureMapper.selectList(pictureQueryWrapper);
        //是否收藏
        LambdaQueryWrapper<GoodsFavorites> favoritesQueryWrapper = new LambdaQueryWrapper<>();
        favoritesQueryWrapper.eq(GoodsFavorites::getUserId, userId);
        favoritesQueryWrapper.eq(GoodsFavorites::getGoodsNo, goods.getGoodsNo());
        Long count = favoritesMapper.selectCount(favoritesQueryWrapper);
        boolean isFavorites = false;
        if (count > 0){
            isFavorites = true;
        }
        //销量
        Long numbers = orderDetailMapper.selectNumbers(goods.getGoodsNo());
        //门店列表
        List<StoreInfoVo> storeList =  storeMapper.selectStoreList(goods.getGoodsNo());

        //组装数据
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        BeanUtils.copyProperties(goods, goodsDetailVo);
        goodsDetailVo.setPictureList(goodsPictures);
        goodsDetailVo.setFavorites(isFavorites);
        goodsDetailVo.setNumbers(numbers);
        goodsDetailVo.setStoreInfoVoList(storeList);
        return goodsDetailVo;
    }
}




